﻿using System;
using System.Collections;
using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

namespace Common
{
    public class FileHelper
    {
        #region 返回文件扩展名，不含“.”
        /// <summary>
        /// /// <summary>
        /// 返回文件扩展名，不含“.”
        /// </summary>
        /// <param name="_filepath">文件全名称</param>
        /// <returns>string</returns>
        public static string GetFileExt(string _filepath)
        {
            if (string.IsNullOrEmpty(_filepath))
            {
                return "";
            }
            if (_filepath.LastIndexOf(".") > 0)
            {
                return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //文件扩展名，不含“.”
            }
            return "";
        }
        #endregion

        #region 通过扩展名检查是否为合法的上传文件（判定危险文件）
        /// <summary>
        /// 通过扩展名检查是否为合法的上传文件（判定危险文件）
        /// </summary>
        public static bool CheckFileExt(string _fileExt)
        {
            //检查危险文件
            string[] excExt = { "asp", "aspx", "ashx", "asa", "asmx", "asax", "php", "jsp", "htm", "html", "dll", "js","exe" };
            for (int i = 0; i < excExt.Length; i++)
            {
                if (excExt[i].ToLower() == _fileExt.ToLower())
                {
                    return false;
                }
            }

            return true;
        }
        #endregion

        #region 根据时间得到目录名 / 格式: yyyyMMdd
        /// <summary>
        /// 根据时间得到目录名yyyyMMdd
        /// </summary>
        /// <returns></returns>
        public static string GetDateDir()
        {
            return DateTime.Now.ToString("yyyyMMdd");
        }
        #endregion

        #region 根据时间得到文件名 / 格式: HHmmssff
        /// <summary>
        /// 根据时间得到文件名HHmmssff
        /// </summary>
        /// <returns></returns>
        public static string GetDateFile()
        {
            return DateTime.Now.ToString("HHmmssff");
        }
        #endregion

        #region 获得当前绝对路径
        /// <summary>
        /// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(IHostingEnvironment HostingEnv, string strPath)
        {
            if (strPath.ToLower().StartsWith("http://"))
            {
                return strPath;
            }
            if (HostingEnv != null)
            {
                return HostingEnv.WebRootPath + $@"\{strPath}"; ;
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
        #endregion

        #region 判断是否是图片文件
        /// <summary>
        /// 是否为图片文件
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        public static bool IsImage(string _fileExt)
        {
            ArrayList al = new ArrayList();
            al.Add("bmp");
            al.Add("jpeg");
            al.Add("jpg");
            al.Add("gif");
            al.Add("png");
            al.Add("ico");
            if (al.Contains(_fileExt.ToLower()))
            {
                return true;
            }
            return false;
        }
        #endregion

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="strPath">文件路径</param>
        public static void DeleteFile( string strPath)
        {
            if(File.Exists(strPath))
            {
                File.Delete(strPath);
            }      
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static bool FileExists(string strPath)
        {
            return File.Exists(strPath);
        }

        /// <summary>
        /// 获取图片高宽度
        /// </summary>
        /// <param name="fileExt"></param>
        /// <param name="filePath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GetImageWidthHeight(string fileExt, string filePath, out int width, out int height)
        {
            width = 0;
            height = 0;
            if (FileHelper.IsImage(fileExt))
            {
                Image imgPhoto = Image.FromFile(filePath);
                width = imgPhoto.Width;
                height = imgPhoto.Height;

                //using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                //{
                //    System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                //    width = image.Width;
                //    height = image.Height;
                //}
            }
        }
    }
}
